#include "CGameOverScene.h"
#include "Constants.h"
#include "GameState.h"
#include "CGameManager.h"
#include "AppDelegate.h"

CCScene* CGameOverLayer::scene() {
	CCScene *scene = CCScene::create();

	CGameOverLayer *layer = CGameOverLayer::create();

	scene->addChild(layer);

	return scene;
}

bool CGameOverLayer::init() {

	if (!CCLayerColor::initWithColor(ccc4(255, 255, 255, 0))) {
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	CCSprite* bgSprite = CCSprite::create(GAMEOVER_TABLE);
	bgSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(bgSprite, indexBackground);

	float bgSpriteX = bgSprite->getPositionX();
	float bgSpriteY = bgSprite->getPositionY();
	float bgSprite_height = bgSprite->getContentSize().height;
	float bgSprite_width = bgSprite->getContentSize().width;
	CCLabelTTF* title = CCLabelTTF::create(GAME_OVER, gameOverFont,
			FONTSIZE_GAMEOVER);
	title->setColor(COLOR_GAMEOVER);
	title->setPosition(
			ccp(visibleSize.width / 2, bgSpriteY + bgSprite_height*3/4));
	this->addChild(title, indexText);

	float titleX = title->getPositionX();
	float titleY = title->getPositionY();

	lbScore = CCLabelTTF::create(SCORE, gameOverFont, FONTSIZE_SCORE);
	lbScore->setColor(COLOR_SCORE);
	updateScore();
	lbScore->setPosition(
			ccp(titleX+bgSprite_width/6, bgSpriteY + bgSprite_height/4));
	this->addChild(lbScore, indexText);

	lbBestScore = CCLabelTTF::create(BEST, gameOverFont, FONTSIZE_SCORE);
	lbBestScore->setColor(COLOR_BEST);
	updateBestScore();
	lbBestScore->setPosition(
			ccp(titleX+bgSprite_width/6, bgSpriteY - bgSprite_height/4));
	this->addChild(lbBestScore, indexText);

	btnPlay = CCSprite::create(GAME_PLAY_BUTTON);
	float playWidth = btnPlay->getContentSize().width;
	float playHeight = btnPlay->getContentSize().height;

	float delta = (visibleSize.width - bgSprite_width) / 2;

	float playX = delta + playWidth / 2;

	btnPlay->setPosition(ccp(playX, bgSpriteY - playHeight - bgSprite_height/3));
	this->addChild(btnPlay);

	btnShare = CCSprite::create(GAME_QUIT_BUTTON);
	float shareX = visibleSize.width - playWidth / 2 - delta;
	btnShare->setPosition(ccp(shareX,bgSpriteY - playHeight - bgSprite_height/3));
	this->addChild(btnShare);

	float faceX = delta + bgSprite_width / 4;
	float faceY = bgSpriteY;
	CCSprite* bgFace;
	if (CCUserDefault::sharedUserDefault()->getBoolForKey(kNewBestScore)
			== true)
		bgFace = CCSprite::create(GAMEOVER_FUN);
	else
		bgFace = CCSprite::create(GAMEOVER_SAD);
	bgFace->setPosition(ccp(faceX, faceY));
	this->addChild(bgFace, indexText);

	this->setTouchEnabled(true);
	//this->setKeypadEnabled(true);

	return true;
}

void CGameOverLayer::updateScore() {
	char *s;
	s = new char[20];

	sprintf(s, "SCORE: %d", GameState::getInstance()->getScores());
	lbScore->setString(s);
	delete[] s;
}
void CGameOverLayer::updateBestScore() {
	char *s;
	s = new char[20];

	sprintf(s, "BEST : %d", GameState::getInstance()->getBestScore());
	lbBestScore->setString(s);
	delete[] s;
}

void CGameOverLayer::keyBackClicked(void) {
	CCLog("keyBackClicked");
	programExit(NULL);
}

void CGameOverLayer::programExit(CCObject* pSender) {
	CGameManager::Instance()->RunScene(SCENE_GAME);
}

void CGameOverLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {
	CCLog("Game Over touched...");
	CCSetIterator i;
	CCTouch *touch;
	CCPoint tap;
	for (i = pTouches->begin(); i != pTouches->end(); i++) {
		touch = (CCTouch*) (*i);
		if (touch) {
			tap = touch->getLocation();
			if (btnPlay->boundingBox().containsPoint(tap)) {
				onPlayClicked();
				break;
			}
			if (btnShare->boundingBox().containsPoint(tap)) {
				onQuitClicked();
				break;
			}
		}
	}
}

void CGameOverLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {

}
void CGameOverLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {

}

void CGameOverLayer::onPlayClicked() {
	keyBackClicked();
}
void CGameOverLayer::onShareClicked() {
	onSaveScreenshot();
	((AppDelegate*) CCApplication::sharedApplication())->shareViaFacebook();
}

void CGameOverLayer::onQuitClicked() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}

void CGameOverLayer::onSaveScreenshot() {
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCRenderTexture* texture = CCRenderTexture::create((int) size.width,
			(int) size.height);
	texture->setPosition(ccp(size.width/2, size.height/2));
	texture->begin();
	CCDirector::sharedDirector()->getRunningScene()->visit();
	texture->end();
	texture->saveToFile("screenshot.png", kCCImageFormatPNG);
}
